From 4c78f628db2b07de8af345b6dd680d0e72b41280 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Thu, 13 Aug 2020 00:45:52 -0400 Subject: [PATCH] gtk-demo: Fontify asynchronously Run highlight asynchronously. It isn't a problem for most demos, but the cursor demo has an unusually large ui file, which takes highlight a little bit of time to produce. --- demos/gtk-demo/main.c | 154 +++++++++++++++++++++++------------------- 1 file changed, 86 insertions(+), 68 deletions(-) diff --git a/demos/gtk-demo/main.c b/demos/gtk-demo/main.c index 551fb034ab..5755070938 100644 --- a/demos/gtk-demo/main.c +++ b/demos/gtk-demo/main.c @@ -8,6 +8,9 @@ #include #include +#include +#include + #include "demos.h" static GtkWidget *info_view; @@ -234,20 +237,81 @@ activate_run (GSimpleAction *action, gtk_demo_run (demo, window); } -static GBytes * -fontify_text (const char *format, - const char *text) +static void +fontify_finish (GObject *source, + GAsyncResult *result, + gpointer data) { - GSubprocess *subprocess; - GBytes *stdin_buf; + GSubprocess *subprocess = G_SUBPROCESS (source); + GtkTextBuffer *buffer = data; GBytes *stdout_buf = NULL; GBytes *stderr_buf = NULL; GError *error = NULL; + + if (!g_subprocess_communicate_finish (subprocess, + result, + &stdout_buf, + &stderr_buf, + &error)) + { + g_clear_pointer (&stdout_buf, g_bytes_unref); + g_clear_pointer (&stderr_buf, g_bytes_unref); + + g_warning ("%s", error->message); + g_clear_error (&error); + + g_object_unref (subprocess); + g_object_unref (buffer); + return; + } + + if (g_subprocess_get_exit_status (subprocess) != 0) + { + if (stderr_buf) + g_warning ("%s", (char *)g_bytes_get_data (stderr_buf, NULL)); + g_clear_pointer (&stderr_buf, g_bytes_unref); + } + + g_object_unref (subprocess); + + g_clear_pointer (&stderr_buf, g_bytes_unref); + + if (stdout_buf) + { + char *markup; + gsize len; + char *p; + GtkTextIter start; + + gtk_text_buffer_set_text (buffer, "", 0); + + /* highlight puts a span with font and size around its output, + * which we don't want. + */ + markup = g_bytes_unref_to_data (stdout_buf, &len); + for (p = markup + strlen ("message); - g_clear_error (&error); - - return NULL; - } - - g_bytes_unref (stdin_buf); - - if (g_subprocess_get_exit_status (subprocess) != 0) - { - if (stderr_buf) - g_warning ("%s", (char *)g_bytes_get_data (stderr_buf, NULL)); - - g_clear_pointer (&stdout_buf, g_bytes_unref); + return; } - g_clear_pointer (&stderr_buf, g_bytes_unref); - - g_object_unref (subprocess); - - return stdout_buf; -} - -void -fontify (const char *format, - GtkTextBuffer *source_buffer) -{ - GtkTextIter start, end; - char *text; - GBytes *bytes; - gtk_text_buffer_get_bounds (source_buffer, &start, &end); text = gtk_text_buffer_get_text (source_buffer, &start, &end, TRUE); + bytes = g_bytes_new_static (text, strlen (text)); - bytes = fontify_text (format, text); - if (bytes) + /* Work around https://gitlab.gnome.org/GNOME/glib/-/issues/2182 */ + if (G_IS_UNIX_OUTPUT_STREAM (g_subprocess_get_stdin_pipe (subprocess))) { - char *markup; - gsize len; - char *p; - - markup = g_bytes_unref_to_data (bytes, &len); - /* highlight puts a span with font and size around its output, - * which we don't want. - */ - for (p = markup + strlen ("